<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all" />
<script src="<?php echo URL::base() ?>media/js/jquery-1.10.1.min.js"></script>
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all" />
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){

        $("input[name$='USD']").keyup(function(){
            checkChinese(this);
        }).blur(function(){
            checkChinese(this);
        });

        $("#supplierbank_add_submit").val('确认提交');
        $(".control-group-USD").hide();
        $('#RMB-account').prop('checked',true);

        /**
         * 开启输入
         */
        function input_enabled(true_or_false)
        {
            $("#payways_id").attr('disabled',!true_or_false);
            $("#bankname-RMB").attr('disabled',!true_or_false);
            $("#bankaccount-RMB").attr('disabled',!true_or_false);
            $("#bankaccountname-RMB").attr('disabled',!true_or_false);

            $("#bankname-USD").attr('disabled',!true_or_false);
            $("#banklocation-USD").attr('disabled',!true_or_false);
            $("#payee-name-USD").attr('disabled',!true_or_false);
            $("#payee-bankaccount-country-USD").attr('disabled',!true_or_false);
            $("#payee-bankaccountname-USD").attr('disabled',!true_or_false);
            $("#payee-bankaccount-address-USD").attr('disabled',!true_or_false);
            $("#payee-bankaccount-num").attr('disabled',!true_or_false);
            $("#payee-address-USD").attr('disabled',!true_or_false);
            $("#payee-bankaccount-code-USD").attr('disabled',!true_or_false);
        }

        /**
         * 开启提交
         */
        function submit_enabled(true_or_false)
        {
            $("#supplierbank_add_submit").attr('disabled',!true_or_false);
        }


        /**
         * 自动提示数据来源
         */
        function aotocomplete_ajax(searchParam, callback,type){
            if(!type )
            {
                type = 'name';
            }
            $.ajax(
                {
                    type: 'get',
                    url: '<?php echo URL::base(true)?>purchase/get_supplier',
                    dataType: 'json',
                    data: {"term": searchParam,"type":type},
                    async: false,
                    success: function (re_data) {
                       //alert(JSON.stringify(re_data));
                       callback(re_data);
                    },
                    error: function () {
                    }
                }
            );
        }

        /**
         * 设置输入框的值并且锁定
         */
        function set_input(data)
        {
            /**
             * 从建议列表中选中之后触发的事件
             */
            $("#supplier_name").attr("readonly", "readonly");
            $("#supplier_id").attr("readonly", "readonly");

            $("#supplier_name").val(data.name);
            $("#supplier_id").val(data.id);
            input_enabled(true);
            submit_enabled(true);
        }
        /**
         * 自动提示功能
         *
         */
        function supplier_name_autocomplete(obj,type){
            var content = {};
            $(obj).autocomplete({
                autoFocus: true,
                minLength: 0,
                autoFill: true,
                source: function(request, callback){
                    var data = request.term;
                    aotocomplete_ajax(data,callback,type);
                },
                response:function(event,ui)
                {
                    content = ui.content[0];
                },
                select: function (event, ui) {

                    set_input(ui.item);
                    /**
                     * 从建议列表中选中之后触发的事件
                     */
                    /*$("#supplier_name").attr("readonly", "readonly");
                    $("#supplier_id").attr("readonly", "readonly");

                    $("#supplier_name").val(ui.item.name);
                    $("#supplier_id").val(ui.item.id);
                    input_enabled(true);
                    submit_enabled(true);*/
                    return false;
                }
            });
            $(obj).blur(function(){
                if(!$("#supplier_id").val()
                    && content
                    && !$("#supplier_name").attr("readonly")
                    && !$("#supplier_id").attr("readonly")
                )
                {
                    set_input(content);
                    /*$("#supplier_name").attr("readonly", "readonly");
                    $("#supplier_id").attr("readonly", "readonly");

                    $("#supplier_name").val(content[0].name);
                    $("#supplier_id").val(content[0].id);
                    input_enabled(true);
                    submit_enabled(true);*/
                    return false;
                }
            });
        }

        /**
         * 输入框获取焦点
         */
        $("#supplier_name").focus(function(){
            var autocomplete = false;

            if($(this).attr('readonly')
            || $(this).data("autocomplete") )
            {
               return autocomplete;
            }
            else
            {
                autocomplete = true;
            }
            if(autocomplete)
            {
                supplier_name_autocomplete(this,'name');
            }
        });

        $("#supplier_id").focus(function(){
            var autocomplete = false;

            if($(this).attr('readonly')
                || $(this).data("autocomplete") )
            {
                return autocomplete;
            }
            else
            {
                autocomplete = true;
            }
            if(autocomplete)
            {
                supplier_name_autocomplete(this,'id');
            }
        });

        /**
         * 重选按钮，刷新页面
         */
        $("#retry_input").click(function(){
            window.location.reload();
            /*if($("#supplier_name").attr("readonly"))
            {
                $("#supplier_name").attr("readonly", false);
            }*/
        });

        input_enabled(false);

        //提交供应商信息按钮
        $("#supplierbank_add_submit").click(function(){
            var coin = $('input[name="currency_account"]:checked ').val();
            if(coin>1){
                if(!checkAtLast()){
                    return;
                }
            }
            var currency_account_id =  $('input[name="currency_account"]:checked ').val();
            var supplier_id = parseInt($('#supplier_id').val());
            var supplier_name = $.trim($('#supplier_name').val());
            var payways_id = $('#payways_id').val();
            var bankname_RMB = $.trim($('#bankname-RMB').val());
            var bankaccount_RMB= $.trim($('#bankaccount-RMB').val());
            var bankaccountname_RMB= $.trim($('#bankaccountname-RMB').val());

            var bankname_USD= $.trim($('#bankname-USD').val());//收款银行名称
            var banklocation_USD= $.trim($('#banklocation-USD').val());//收款银行地区
            var payee_address_USD = $.trim($('#payee-address-USD').val());//收款人地址
            var payee_name_USD= $.trim($('#payee-name-USD').val());//收款人名称
            var payee_bankaccount_address_USD = $.trim($('#payee-bankaccount-address-USD').val());//收款银行地址
            var payee_bankaccount_num = $.trim($('#payee-bankaccount-num').val());//收款人账户号码/IBAN
            var payee_bankaccount_code_USD = $.trim($('#payee-bankaccount-code-USD').val());//银行代码SWIFT

            if(supplier_id==0 || supplier_name=='' || payways_id==0){
                alert('请填写正确的供应商id、填写正确的供应商名称、选择正确的付款方式');
                return false;
            }
            if(currency_account_id==1){
                if(bankname_RMB=='' || bankaccount_RMB=='' || bankaccountname_RMB==''){
                    alert('开户银行、银行帐号、开户名称不能为空');
                    return false;
                }
            }else{
                if(bankname_USD=='' || payee_address_USD=='' || payee_name_USD=='' || payee_bankaccount_address_USD=='' ||
                    payee_bankaccount_num=='' || payee_bankaccount_code_USD=='' || banklocation_USD==''){
                    alert('银行信息存在未填写项目，请将银行信息填写完整');
                    return false;
                }
            }
            var bank_info = '{';
            bank_info += '"supplier_id":"'+supplier_id+'",';
            bank_info += '"supplier_name":"'+supplier_name+'",';
            bank_info += '"payways_id":"'+payways_id+'",';

            if(currency_account_id==1){
                bank_info += '"bankname_RMB":"'+bankname_RMB+'",';
                bank_info += '"bankaccount_RMB":"'+bankaccount_RMB+'",';
                bank_info += '"bankaccountname_RMB":"'+bankaccountname_RMB+'"}';
            }else{
                bank_info += '"bankname_USD":"'+bankname_USD+'",';
                bank_info += '"banklocation_USD":"'+banklocation_USD+'",';
                bank_info += '"payee_address_USD":"'+payee_address_USD+'",';
                bank_info += '"payee_name_USD":"'+payee_name_USD+'",';
                bank_info += '"payee_bankaccount_address_USD":"'+payee_bankaccount_address_USD+'",';
                bank_info += '"payee_bankaccount_num":"'+payee_bankaccount_num+'",';
                bank_info += '"payee_bankaccount_code_USD":"'+payee_bankaccount_code_USD+'"}';
            }
            $.post(
                "<?php echo URL::base() ?>supplier/supplierbank/doadd",
                {currency_account_id:currency_account_id,bank_info:bank_info},
                function(data){
                    console.log(data);

                    if(!isNaN(data)){
                        window.location.href="<?php echo URL::site(); ?>supplier/supplierbank/detail?id="+data;

                    }else{
                        alert(data);
                    }
                });
        });

    })

    /**
     *人名币/美元的显示或引藏信息替换
     */
    function hidden_USD(){
        $(".control-group-USD").hide();
        $(".control-group-RMB").show();

    }
    function show_USD(){
        $(".control-group-USD").show();
        $(".control-group-RMB").hide();

    }
    function show_HKD(){
        $(".control-group-USD").show();
        $(".control-group-RMB").hide();
    }
    function clearDou(obj) {
        obj.value = obj.value.replace(/[，]/g, "");  //清除“数字”和“.”以外的字符
    }

    function checkChineseFunction(val){
        var coin = $('input[name="currency_account"]:checked ').val();
        if(coin>1){
            for(var i = 0; i < val.length; i ++) {
                if(val.charCodeAt(i) > 127){
                    alert("输入信息包含中文字符 \'" + val[i] + "\' 请仔细审核下");
                    return false;
                    break;
                }
            }
        }
    }

    function checkChinese(obj){
        var val = $(obj).val();
        if(val){
            var coin = $('input[name="currency_account"]:checked ').val();
            if(coin>1){
                for(var i = 0; i < val.length; i ++) {
                    if(val.charCodeAt(i) > 127){
                        alert("输入信息包含中文字符 \'" + val[i] + "\' 请仔细审核下");
                        return false;
                    }
                }
                return true;
            }
        }
    }

    function checkAtLast(){
        var f = true;
        $("input[name$='USD']").each(function(){
            if(!checkChinese(this)){
                f = false;
                return;
            }
        });
        return f;
    }
</script>
<?php if(isset($add_id) and $add_id > 0){?>
<div>
    添加成功
    <a href="<?php echo url::base(true).$add_id; ?>">查看</a>
</div>
<?php }?>
<!--<form action="--><?php //echo url::base(true)?><!--supplier/supplierbank/doadd" method="post" id="supplierbank_add_form">-->
    <fieldset>

     <div class="control-group">
            <input type="radio" value=1 checked="checked" name="currency_account" class="currency_account" id="RMB-account" onclick="hidden_USD()" />人名币帐号
            <input type="radio" value=2 name="currency_account" class="currency_account" onclick="show_USD()" />美元帐号
            <input type="radio" value=3 name="currency_account" class="currency_account" onclick="show_HKD()" />港币帐号

     </div>

    <div class="control-group">
        <label class="control-label" for="supplier_id">供应商ID：</label>
        <div class="controls">
            <input type="text" placeholder="请输入供应商ID" class="input-xlarge"  autocomplete="off"
                   id="supplier_id"       name="supplier_id">
            <p class="help-block"></p>
        </div>
    </div>

    <div class="control-group">
            <label class="control-label" for="supplier_name">供应商名称：</label>
            <div class="controls">
                <input type="text" placeholder="请输入供应商名称" class="input-xlarge" autocomplete="off"
                       id="supplier_name"       name="supplier_name">
                <button type="button" id="retry_input">重选</button>
                <p class="help-block"></p>
            </div>
    </div>

    <div class="control-group">
            <label class="control-label" for="payways_id">付款方式：</label>
            <div class="controls">
                <select name="payways_id" disabled="disabled" id="payways_id">
                    <option value="0">请选择付款方式</option>
                    <?php
                    if(isset($payways_all) and $payways_all){
                        foreach($payways_all as $payway){?>
                    <option value="<?php echo $payway['id']?>"><?php echo $payway['paywayname']?></option>
                    <?php }
                    }?>
                </select>
                <p class="help-block"></p>
            </div>
    </div>
        <!-- 人名币帐号新增内容-->
    <div class="control-group-RMB">
            <label class="control-label" for="bankname">开户银行：</label>
            <div class="controls">
                <input type="text" placeholder="开户银行" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="bankname-RMB" id="bankname-RMB" required="required">
                <p class="help-block"></p>
            </div>
    </div>

    <div class="control-group-RMB">
            <label class="control-label" for="bankaccount">银行帐号：</label>
            <div class="controls">
                <input type="text" placeholder="请输入银行帐号" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="bankaccount-RMB" id="bankaccount-RMB" required="required">
                <p class="help-block"></p>
            </div>
    </div>

    <div class="control-group-RMB">

            <label class="control-label" for="bankaccountname">开户名称：</label>
            <div class="controls">
                <input type="text" placeholder="请输入开户名称" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="bankaccountname-RMB" id="bankaccountname-RMB" required="required">
                <p class="help-block"></p>
            </div>
    </div>
        <!-- 美元帐号新增内容-->
        <div class="control-group-USD">
            <!-- Text input-->
            <label class="control-label" for="bankname">受款银行名称：</label>
            <div class="controls">
                <input type="text" placeholder="受款银行名称" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="bankname-USD" id="bankname-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>

        <div class="control-group-USD">
            <!-- Text input-->
            <label class="control-label" for="bankname">受款国家地区(国家、省、城市)：</label>
            <div class="controls">
                <input type="text" placeholder="受款国家地区" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="banklocation-USD" id="banklocation-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>

        <div class="control-group-USD">
            <label class="control-label" for="payee-bankaccount-address">受款银行地址：</label>
            <div class="controls">
                <input type="text" placeholder="请输入受款银行地址" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="payee-bankaccount-address-USD" id="payee-bankaccount-address-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>
        <div class="control-group-USD">

            <label class="control-label" for="payee-bankaccount-num">受款人账户号码/IBAN：</label>
            <div class="controls">
                <input type="text" placeholder="请输入受款人账户号码/IBAN" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="payee-bankaccount-num-USD" id="payee-bankaccount-num" required="required">
                <p class="help-block"></p>
            </div>
        </div>
        <div class="control-group-USD">
            <label class="control-label" for="payee-name">受款人名称：</label>
            <div class="controls">
                <input type="text" placeholder="请输入受款人名称" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="payee-name-USD" id="payee-name-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>
        <div class="control-group-USD">

            <label class="control-label" for="payee-address">受款人地址：</label>
            <div class="controls">
                <input type="text" placeholder="请输入受款人地址" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="payee-address-USD" id="payee-address-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>
        <div class="control-group-USD">

            <label class="control-label" for="payee-bankaccount-code">银行代码SWIFT：</label>
            <div class="controls">
                <input type="text" placeholder="请输入银行代码SWIFT" class="input-xlarge" disabled="disabled" autocomplete="off"
                       name="payee-bankaccount-code-USD" id="payee-bankaccount-code-USD" required="required">
                <p class="help-block"></p>
            </div>
        </div>

    <div class="controls">
            <input type="submit" class="btn btn-info" id="supplierbank_add_submit"
                   disabled="disabled"
                   value="确认提交" name="add_submit">
    </div>


        </fieldset>
<!--</form>-->
